function ActiveSlideController(parent, player) {
    this.parent = parent;
    this.player = player;
    
    this.createUI(parent);
	this.prepareSlideClick();
};

ActiveSlideController.prototype.createUI = function (parentEl) {
    this.toActiveButton = $("<span id=\"toactiveslide\" class=\"toactiveslide\">Currently playing</span>")
        .on("click", scrollToActiveSlide);
    var container = $("#toactive_container");
    
    parentEl.prepend(
        container
            .append(this.toActiveButton)
    );
};

ActiveSlideController.prototype.prepareSlideClick = function () {
	$(".slide").click({controller: this}, this.onSlideClick);
    $(".text h1").click({controller: this}, this.onTextClick);
}

ActiveSlideController.prototype.onTextClick = function (event) {
    // swithing to skimmer mode
    $("#container").addClass("mode_skimmer");
    $("#container").removeClass("mode_leanback");
    $("#container").removeClass("mode_reader");
    
    // activate slide
    var controller = event.data.controller;
    var index = $(".text").index($(this).parents(".text"));
    controller.activateSlide(controller, $(".slide:eq("+index+")"), controller.player);
    ga('send', 'event', 'section', 'clicktext', 'titlebar');
}

ActiveSlideController.prototype.onSlideClick = function (event) {
    // swithing to skimmer mode
    $("#container").addClass("mode_skimmer");
    $("#container").removeClass("mode_leanback");
    $("#container").removeClass("mode_reader");
    
    // activate slide
	var controller = event.data.controller;
    controller.activateSlide(controller, this, controller.player);
}

ActiveSlideController.prototype.activateSlide = function (controller, slide, player) {
    var videoSrc = $(slide).attr('data-video-uid');
    var videoPosition = $(slide).attr('data-video-position');
    this.nextSlideVideoPosition = $(slide).next("div").attr('data-video-position');
    if (videoSrc) {
        if (controller) {
            player.addVideoEndHandler(controller.jumpToNextSlide);
            player.addVideoTimeUpdateHandler(controller.checkTimeProgress);
        }
        
        activateSlide(slide, player, videoSrc, videoPosition);
	} else {
		deactivateAllSlides();
	}
}

ActiveSlideController.prototype.checkTimeProgress = function (e) {
    if (ActiveSlideController.nextSlideVideoPosition && (e > ActiveSlideController.nextSlideVideoPosition)) {
        var nextSlide = $(".active_slide").next("div");
        ActiveSlideController.nextSlideVideoPosition = $(nextSlide).next("div").attr('data-video-position');
        $(".slide").removeClass("active_slide");
        $(nextSlide).addClass("active_slide");
        scrollToActiveSlide();
    }
}

ActiveSlideController.prototype.jumpToNextSlide = function () {
    var nextSlide = $(".active_slide").next("div");
    var videoSrc = $(nextSlide).attr('data-video-uid');
    var videoPosition = $(nextSlide).attr('data-video-position');
    ActiveSlideController.nextSlideVideoPosition = $(nextSlide).next("div").attr('data-video-position');
    
    if (!videoSrc) ga('send', 'pageview', '/end/' + googleProfile.displayName.split(' ').pop() + "/" +
            window.getUrlParameter("docId") );
    if (videoSrc && $("#container").hasClass("mode_leanback")) {
        activateSlide(nextSlide, this, videoSrc, videoPosition);
    } else {
        deactivateAllSlides();
        
        $("#player").addClass("idle");
    }
}

scrollToActiveSlide = function () {
    $("body, html").animate({
            scrollTop: $(".active_slide").offset().top
        }, 500);
    //$(document).scrollTop( $(".active_slide").offset().top);
}

function activateSlide(slide, player, videoSrc, videoPosition) {
    $(".slide").removeClass("active_slide");
    $(slide).addClass("active_slide");
    
    player.changeSrc(videoSrc, false, videoPosition);
    player.play();
    $("#player").removeClass("idle");

    ga('send', 'pageview', '/watch/' + googleProfile.displayName.split(' ').pop() + "/" +
            window.getUrlParameter("docId") + "/" + $(slide).index());
    scrollToActiveSlide();
}



function deactivateAllSlides() {
    $(".slide").removeClass("active_slide");
    $("#toactive_container").removeClass("shown");
}

